package date;

import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @description: String常用的方法
 * @author: apple
 * @date:  2021/12/8 2:56 下午
 * @version 1.0
 **/
public class StringTest {


    @Test
    public void test6() {

        String s1 = "ab";
        String s2 = "cd";

        String s3 = "abcd";

        String s4 = s1 + s2;

        System.out.println(s4 == s3);
        System.out.println(s4.equals(s3));

        final String s5 = "ab";
        final String s6 = "cd";
        String s7 = s5 + s6;

        System.out.println(s7 == s3);
        System.out.println(s7.equals(s3));
    }

    @Test
    public void test5() {
        // 字符串转byte数组
        String str = "abc";
        byte[] bys = str.getBytes();
        System.out.println(Arrays.toString(bys));
        try {
            // byte数组转String
            String s = new String(bys , StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test4() {
        // char[]转字符串
        char[] chars = {'a' , 'b' , 'c'};
        String str = new String(chars);
        System.out.println(str);
    }

    @Test
    public void test3() {

        // 字符串转char[]
        String str = "abc";
        char[] chars = str.toCharArray();
        System.out.println(Arrays.toString(chars));

    }

    @Test
    public void test1() {

        int a = 5;
        int b = 5;

        // 自动装箱
        Integer a1 = 5;
        Integer a2 = 5;

        // 自动装箱
        Integer aa1 = Integer.valueOf(5);
        Integer aa2 = Integer.valueOf(5);

        // 重新分配空间
        Integer aaa1 = new Integer(5);
        Integer aaa2 = new Integer(5);

        System.out.println(a == b);
        System.out.println(a1 == a2);
        System.out.println(aa1 == aa2);
        System.out.println(aaa1 == aaa2);
    }

    @Test
    public void test2() {

        // 开辟了一块空间
        String s1 = "abc";
        String s2 = "abc";

        // 开辟了两块空间
        String ss1 = new String("abc");
        String ss2 = new String("abc");

        String newStr = s1.replace(s1, "def");



        System.out.println(s1 == s2);
        System.out.println(ss1 == ss2);

        System.out.println(newStr);
        System.out.println(s1);
    }
}
